//
//  FDCityViewController.m
//  FamousDoctorOnline
//
//  Created by 应谨鸿 on 16/7/6.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDCityViewController.h"
#import "FDProvinceViewController.h"
#import "FDProvinceModel.h"
#import "WZHUserDefaults.h"

#define kCity @"City"
@implementation FDCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择当前城市位置";
    
    self.view.backgroundColor = WHITE_COLOR;
    
    //设置导航栏上面的字体颜色
    [self.navigationController.navigationBar setTintColor:WHITE_COLOR];
    
    //设定统一的导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:kThemeColor];
    
    //统一设定导航控制器左边的item的样式
    [self setupLeftNavBarButtonItem];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];}
    
    NSString *model = self.dataArray[indexPath.row];
    
    cell.textLabel.text = model;
    
    return cell;
}

/**
 *  cell点击
 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *city = self.dataArray[indexPath.row];
    
//    存储城市数据
    [WZHUserDefaults WZHSaveObject:city forKey:kCity];
    
    UIViewController *vc = self.navigationController.childViewControllers.firstObject;
    //用sel类型包装方法
    SEL sel = NSSelectorFromString(@"loadCurDayWeather:");
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    //判断是否有名字封装
    if ([vc respondsToSelector:sel]) {
        [vc performSelector:sel withObject:city];
    }else{
    
        NSLog(@"error");
    }
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/**
 * 重写set
 */
- (void)setDataArray:(NSArray *)dataArray{
    
    _dataArray = dataArray;
    
    [self.tableView reloadData];
}

/**
 *  item设置
 */
- (void)setupLeftNavBarButtonItem{
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"JianTou"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    
    self.navigationItem.leftBarButtonItem = item;
    
}

/**
 *  pop返回
 */
- (void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
